package com.wisdytech.linkdcs.equipLifeCycle.malfunction.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.enums.MalfunctionStatusEnum;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.dao.IMalfunctionRecordDao;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.dto.MalfunctionSearchDto;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MAttachment;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MalfunctionRecord;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.service.IMAttachmentService;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.service.IMalfunctionRecordService;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.vo.MRegisterVo;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.vo.MalfunctionVo;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dailibo
 * @since 2018-06-11
 */
@Service
@Transactional
public class MalfunctionRecordServiceImpl extends BaseService<IMalfunctionRecordDao, MalfunctionRecord> implements IMalfunctionRecordService {

	private Logger logger =LoggerFactory.getLogger(getClass());
	
	@Autowired
	private IMalfunctionRecordDao malfunctionRecordDao;
	
	@Autowired
	private IMAttachmentService attachmentService;
	
	
	@Override
	public PageInfo<MalfunctionVo> selectAllMalfunctionLists(Filter filter, MalfunctionSearchDto dto) {
		
		List<MalfunctionVo> result =
				this.beforeList(IMalfunctionRecordDao.class, filter).selectMRecords(dto);
		
		return new PageInfo<>(result);
	}

	@Override
	public boolean add(MalfunctionRecord record, String userName) {
		record.setPkId(BaseUtil.getUUID());
		record.setDelFlag(CommonConstants.DEL_FLAG);
		record.setCreateUser(userName);
		record.setCreateDate(new Date());
		record.setUpdateDate(new Date());
		record.setFailureStatus(MalfunctionStatusEnum.register.getCode());
		
		//增加附件表
		MAttachment  attachment=new MAttachment();
		attachment.setAttachmentName(record.getFileName());
		attachment.setAttachmentPath(record.getFilePath());
		attachment.setRecordId(record.getPkId());
		attachmentService.add(attachment);
		
		
		return record.insert();
	}

	@Override
	public boolean submint(String recordIds, String userName) {
		String[]  recordIdsList=
				recordIds.split(",");
		for(String recordId : recordIdsList) {
			MalfunctionRecord record =new MalfunctionRecord();
			record.setPkId(recordId);
			record.setUpdateUser(userName);
			record.setUpdateDate(new Date());
			record.setFailureStatus(MalfunctionStatusEnum.deal.getCode());
			//判断更新是否成功
			if(!record.updateById()) {
				throw new ServiceException("提交故障登记失败");
			}
		}
		
		return true;
	}

	@Override
	public MRegisterVo getRecordById(String pkId) {
		
		 return malfunctionRecordDao.selectRegisterDetail(pkId);
	}

}
